package com.atguigu.vod.service.impl;

import com.alibaba.nacos.client.naming.utils.StringUtils;
import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.*;
import com.atguigu.commonutils.R;
import com.atguigu.vod.service.VodService;
import com.atguigu.vod.utils.ConstantPropertiesUtil;
import com.atguigu.vod.utils.AliyunVodSDKUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Service
public class VodServiceImpl implements VodService {

    /*这个视频上传采用的是流式上传接口*/
    @Override
    public R testUploadVideo(MultipartFile multipartFile) throws IOException {
        String accessKeyId=ConstantPropertiesUtil.ACCESS_KEY_ID;
        String accessKeySecret=ConstantPropertiesUtil.ACCESS_KEY_SECRET;
        String fileName=multipartFile.getOriginalFilename();
        String title=fileName.substring(0, fileName.lastIndexOf("."));
        InputStream inputStream = multipartFile.getInputStream();

        UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret, title, fileName, inputStream);

        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadStreamResponse response = uploader.uploadStream(request);
        System.out.print("RequestId=" + response.getRequestId() + "\n");  //请求视频点播服务的请求ID
        if (response.isSuccess()) {
            System.out.print("VideoId=" + response.getVideoId() + "\n");
        } else { //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
            System.out.print("VideoId=" + response.getVideoId() + "\n");
            System.out.print("ErrorCode=" + response.getCode() + "\n");
            System.out.print("ErrorMessage=" + response.getMessage() + "\n");
        }
        return R.ok().data("videoId", response.getVideoId());
    }

    /*根据视频id删除阿里云里面的视频*/
    @Override
    public boolean removeAliVideo(String videoId) {
        System.out.println("传递过来的videoId："+videoId);
        DefaultAcsClient client = null;
        try {
            client = AliyunVodSDKUtils.initVodClient(ConstantPropertiesUtil.ACCESS_KEY_ID, ConstantPropertiesUtil.ACCESS_KEY_SECRET);
        } catch (Exception e) {
            e.printStackTrace();
        }
        DeleteVideoResponse response = new DeleteVideoResponse();
        DeleteVideoRequest request = new DeleteVideoRequest();
        request.setVideoIds(videoId);
        try {
            response = client.getAcsResponse(request);
        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
        }
        if(!StringUtils.isEmpty(response.getRequestId())){
            return true;
        }
        return false;
    }

    /*根据视频的id集合删除阿里云里面的视频*/
    @Override
    public boolean removeAliVideos(ArrayList<String> videoIds) {
        System.out.println("传递过来的videoIds是："+videoIds);
        /*对 videoIds 进行 处理*/
        String join = org.apache.commons.lang.StringUtils.join(videoIds.toArray(),',');
        System.out.println("====: "+join);
        DefaultAcsClient client = null;
        client = AliyunVodSDKUtils.initVodClient(ConstantPropertiesUtil.ACCESS_KEY_ID, ConstantPropertiesUtil.ACCESS_KEY_SECRET);
        DeleteVideoResponse response = new DeleteVideoResponse();
        DeleteVideoRequest request = new DeleteVideoRequest();
        /*如果要放ids,每个id之间要用逗号隔开*/
        request.setVideoIds(join);
        try {
            response = client.getAcsResponse(request);
        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
        }
        return true;
    }

    /*根据视频id获取视频播放凭证，传递给前端播放视频*/
    @Override
    public R getPlayAuth(String videoSourceId) {
        /*1.获取阿里云的秘钥和id*/
        String AccessKeyId=ConstantPropertiesUtil.ACCESS_KEY_ID;
        String AccessKeySecret=ConstantPropertiesUtil.ACCESS_KEY_SECRET;
        /*2.得到初始化的客户端*/
        DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(AccessKeyId, AccessKeySecret);
        /*3.初始化response、request*/
        GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(videoSourceId);
        String playAuth=null;
        try {
            /*4.获取播放凭证*/
            response = client.getAcsResponse(request);
            playAuth = response.getPlayAuth();
            System.out.println("获取到的playAuth1:"+playAuth);
            System.out.println("播放凭证："+response.getPlayAuth()+"\n");
            System.out.println("视频标题："+response.getVideoMeta().getTitle()+"\n");
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return R.ok().message("获取凭证成功").data("playAuth", playAuth);
    }


}